/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: tabwidget.cpp 534 2009-05-14 19:54:59Z jpnurmi $
*/

#include "tabwidget.h"
#include "sharedtimer.h"
#include <QApplication>
#include <QStyle>

static QIcon emptyIcon;

TabWidget::TabWidget(QWidget* parent) : QTabWidget(parent)
{
    // workaround for jumping tab bar: tab icons are too big by default
    // -> the tab bar gets resized when icons appear
    int s = style()->pixelMetric(QStyle::PM_TabBarIconSize, 0, this) - 2;
    setIconSize(QSize(s, s));

    d.alertIcon = QApplication::windowIcon();
    d.highlightIcon = QApplication::windowIcon();

    if (emptyIcon.isNull())
    {
        QPixmap pixmap(iconSize());
        pixmap.fill(Qt::transparent);
        emptyIcon.addPixmap(pixmap);
    }
}

QIcon TabWidget::alertIcon() const
{
    return d.alertIcon;
}

void TabWidget::setAlertIcon(const QIcon& icon)
{
    d.alertIcon = icon;
}

QIcon TabWidget::highlightIcon() const
{
    return d.highlightIcon;
}

void TabWidget::setHighlightIcon(const QIcon& icon)
{
    d.highlightIcon = icon;
}

bool TabWidget::hasTabAlert(int index)
{
    return d.alertIndexes.contains(index);
}

void TabWidget::setTabAlert(int index, bool alert)
{
    if (!alert)
    {
        int count = d.alertIndexes.removeAll(index);
        if (count > 0 && d.alertIndexes.isEmpty())
        {
            emit alertStatusChanged(false);
            SharedTimer::instance()->unregisterReceiver(this, "alertTimeout");
        }
    }
    else
    {
        if (d.alertIndexes.isEmpty())
        {
            emit alertStatusChanged(true);
            SharedTimer::instance()->registerReceiver(this, "alertTimeout");
        }
        if (!d.alertIndexes.contains(index))
            d.alertIndexes.append(index);
    }
}

bool TabWidget::hasTabHighlight(int index) const
{
    return d.highlightIndexes.contains(index);
}

void TabWidget::setTabHighlight(int index, bool highlight)
{
    if (!highlight)
    {
        int count = d.highlightIndexes.removeAll(index);
        if (count > 0 && d.highlightIndexes.isEmpty())
            emit highlightStatusChanged(false);
        setTabIcon(index, QIcon());
    }
    else
    {
        if (d.highlightIndexes.isEmpty())
            emit highlightStatusChanged(true);
        if (!d.highlightIndexes.contains(index))
            d.highlightIndexes.append(index);
        setTabIcon(index, d.highlightIcon);
    }
}

void TabWidget::moveToNextTab()
{
    int index = currentIndex();
    if (++index >= count())
        index = 0;
    setCurrentIndex(index);
}

void TabWidget::moveToPrevTab()
{
    int index = currentIndex();
    if (--index < 0)
        index = count() - 1;
    setCurrentIndex(index);
}

static void shiftIndexesFrom(QList<int>& indexes, int from, int delta)
{
    QMutableListIterator<int> it(indexes);
    while (it.hasNext())
    {
        if (it.next() >= from)
            it.value() += delta;
    }
}

void TabWidget::tabInserted(int index)
{
    shiftIndexesFrom(d.alertIndexes, index, 1);
    shiftIndexesFrom(d.highlightIndexes, index, 1);
}

void TabWidget::tabRemoved(int index)
{
    shiftIndexesFrom(d.alertIndexes, index, -1);
    shiftIndexesFrom(d.highlightIndexes, index, -1);
}

void TabWidget::alertTimeout()
{
    QIcon icon;
    if (!d.alertIndexes.isEmpty())
    {
        icon = tabIcon(d.alertIndexes.first());
        if (icon.cacheKey() == emptyIcon.cacheKey())
            icon = d.alertIcon;
        else
            icon = emptyIcon;
    }

    foreach (int index, d.alertIndexes)
        setTabIcon(index, icon);
}
